<?php 
class annonce_models_chercher extends CI_Model{
	public function __construct(){
		$this->load->database();
	} 
	/*
   public function get_annonces($departement=array(),$codePostal='',$category='',$rechercher='',$type='offre',$option=array(),$shop_id='',$company='',$page=1,$limit=25){
   		$prd_id=self::search_by_words($rechercher);

   		if(empty($prd_id) and $rechercher!='') return false;
   		
	   	$this->db->select('prd_id')->from('product')->where('prd_active', 1)->where( 'prd_type', $type )->limit( $limit,$page);
   		
   		if(is_array($departement) and !empty($departement)) $this->db->where_in( 'prd_departement', $departement );

   		if(strlen($codePostal)==5) $this->db->where_in( 'prd_codepostal', $codePostal );
   		if($category!='') $this->db->where( 'category_id', $category );
   		if($shop_id!='') $this->db->where( 'shop_id', $shop_id );
   		if(!empty($prd_id)) $this->db->where_in( 'prd_id', $prd_id );
   		if(is_numeric($company)) $this->db->where( 'company', $company );
   		$query = $this->db->get();
   		
   		if($query->num_rows()){
   			$annonces=array();
   			foreach ( $query->result() as $row)
   				$annonces[]= $row->prd_id; 
   			return $annonces;
   		}
   		return false;
   }
   public function search_by_words($rechercher){
		//separer mot par mot
		if(trim($rechercher)!='')
		   	foreach (explode(' ',$rechercher) as $word){
		   		if(strlen ($word)>2)
		   			$keyword[$word]=implode('',self::mbStringToArray($word));
		   	}
	   	
	   	if(isset($keyword)){
	   		foreach ($keyword as $word=>$sortWord){
	   			$query = $this->db->get_where('index_product_word', array('word' => $word));
	   			//si le mot est existe si non recherche faute d'othographe
	   			if( $query->num_rows()){
	   				foreach ( $query->result() as $row)
			   			$id_word[$word]= $row->id;
	   			}else{
	   				$this->db->select('id')->from('index_product_word')->where_in('sort',$keyword);
			   		$query = $this->db->get();
			   		foreach ( $query->result() as $row)
			   			$id_word[$word]= $row->id;
	   			}
	   		}
	   		
	   			
	   	}
	   	//chercher le mot dans product trier par son poids
	   	if(isset($id_word)){
	   		$id_word=array_unique($id_word);
	   		$this->db->select('id_doc, sum(weight) as total')->from('index_word')->
	   		where_in('id_word',$id_word)->
	   		group_by("id_doc")->
	   		order_by('total DESC');
	   		$query = $this->db->get();
	   		foreach ( $query->result() as $row)
	   			$prd_id[]= $row->id_doc;
	   	}
	   	if(isset($prd_id)) return $prd_id;
	   	return array();
   }
   private function mbStringToArray( $string){
   	$string = htmlentities($string, ENT_NOQUOTES, 'utf-8');
   
   	// remplacer les entités HTML pour avoir juste le premier caractères non accentués
   	// Exemple : "&ecute;" => "e", "&Ecute;" => "E", "Ã " => "a" ...
   	$string = preg_replace('#&([A-za-z])(?:acute|grave|cedil|circ|orn|ring|slash|th|tilde|uml);#', '\1', $string);
   	// Supprimer tout le reste
   	$string = preg_replace('#&[^;]+;#', '', $string);
   	$sort_word=str_split($string);
   	sort($sort_word);
   	return $sort_word;
   }
   public function get_nb_annonces($departement=array(),$codePostal='',$category='',$rechercher='',$type='offre',$option=array(),$is_shop=''){
   		$prd_id=self::search_by_words($rechercher);
	   	if(empty($prd_id) and $rechercher!='') return 0;
	   	
   		$this->db->select('count(prd_id) as nb')->from('product')->where('prd_active', 1)->where( 'prd_type', $type );
	   	
	   	if(is_array($departement) and !empty($departement)) $this->db->where_in( 'prd_departement', $departement );

	   	if(strlen($codePostal)==5) $this->db->where_in( 'prd_codepostal', $codePostal );
	   	if($category!='') $this->db->where( 'category_id', $category );
	   	if(is_numeric($is_shop)) $this->db->where( 'company', $is_shop );
	   	if(!empty($prd_id)) $this->db->where_in( 'prd_id', $prd_id );
	   	
	   	$query = $this->db->get(); 
	   	$row=$query->result();
	   	return $row[0]->nb; 
   }*/
   public function annonces_by_email($email){
   		$query = $this->db->get_where('product', array('user_email' => $email));
   		if( $query->num_rows()){
   			foreach ( $query->result() as $row)
   				$annonces[]= $row->prd_id;
   			if(isset($annonces)) return $annonces;
   		}
   		return false;
   }
}